<template>
  <div class="home-container">
    <van-nav-bar class="page-nav-bar">
      <van-button slot="title" class="searchBtn" icon="search" size="small" to="/search" @click="searchBtn">搜索</van-button>
    </van-nav-bar>
    <van-tabs v-model="active" swipeable animated class="tabs">
      <van-tab v-for="item in channels" :key="item.id" :title="item.name">
        <article-list :channel="item"></article-list>
      </van-tab>
      <div slot="nav-right" class="placeholder"></div>
      <div slot="nav-right" class="hamburger">
        <i slot="title" class="iconfont icon-gengduo" @click="popUpShow=true"></i>
      </div>
    </van-tabs>
    <!-- 底部弹层 -->
    <van-popup
      v-model="popUpShow"
      closeable
      close-icon-position="top-left"
      position="bottom"
      round
      :style="{ height: '94%' }"
    ><channel-edit :user-channel="channels" :active-index="active" @changeActive="changeActive($event)"></channel-edit>
    </van-popup>
  </div>
</template>

<script>
import { getUserChannels } from '@/api/user.js'
import ArticleList from './articleList.vue'
import channelEdit from './channel-edit.vue'
import { mapState } from 'vuex'
import { getToken } from '@/utils/storage.js'
export default {
  name: 'HomeIndex',
  components: {
    ArticleList,
    channelEdit
  },
  data() {
    return {
      active: 0,
      channels: [],
      popUpShow: false
    }
  },
  computed: {
    ...mapState(['token'])
  },
  created() {
    this.getChannels()
  },
  mounted() {},
  methods: {
    searchBtn() {},
    async getChannels() {
      try {
        const {
          data: {
            data: { channels }
          }
        } = await getUserChannels()
        console.log(channels)
        if (this.token) {
          this.channels = channels
        } else {
          this.channels = getToken('MY-CHANNEL')
        }
      } catch (error) {
        this.$toast.fail('获取频道列表失败')
      }
    },
    changeActive(index, status = true) {
      this.active = index
      this.popUpShow = status
    }
  }
}
</script>

<style scope lang="less">
.home-container {
  padding-bottom: 100px;
  .page-nav-bar {
    width: 100%;
    position: sticky;
    top: 0;
    left: 0;
    .van-nav-bar__title {
      max-width: 100%;
    }
    .searchBtn {
      width: 555px;
      height: 64px;
      border-radius: 32px;
      background-color: #5babfb;
      color: #fff;
      font-size: 28px;
      border: none;
      .van-icon-search {
        color: #fff;
        font-size: 32px;
      }
    }
  }
  .tabs {
    .van-tabs__wrap {
      // position: fixed;
      // top: 92px;
      z-index: 10;
    }
    .van-tabs__nav {
      padding: 0;
    }
    .van-tab {
      width: 202px;
      height: 83px;
      font-size: 28px;
      color: #777;
      border-bottom: 1px solid #edeff3;
      border-left: 1px solid #edeff3;
    }
    .van-tabs__line {
      bottom: 10px;
      width: 31px;
      height: 6px;
      background-color: #3296fa;
      .van-tabs__wrap {
        height: 0;
      }
    }
    .van-tab--active {
      color: #000;
      font-size: 30px;
    }
    .hamburger {
      position: fixed;
      right: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      width: 65px;
      height: 82px;
      background-color: rgba(255, 255, 255, 0.9);
      i {
        font-size: 33px;
      }
      &:before {
        content: '';
        display: inline-block;
        position: absolute;
        left: 0;
        height: 75px;
        width: 1px;
        background: url('~@/assets/gradient-gray-line.png');
        background-size: contain;
      }
    }
    .placeholder {
      width: 65px;
      flex-shrink: 0;
    }
  }
}
</style>
